#!/usr/bin/perl -w
$|++;
use strict;
use Switch;
use File::Basename;

my $fichier_conf = dirname(__FILE__) . "/compiz.bindings";
my $fifo_file = "/tmp/wm_binding.fifo";


print "conf_file:  $fichier_conf \n";

our %config;

reloadConf();



unless(-p $fifo_file){
  system('mknod', $fifo_file, 'p')
		&& die "can't mknod $fifo_file: $!";
}

my $fifo_fh;
open($fifo_fh, "+< $fifo_file") or die "The FIFO file \"$fifo_file\" is missing, and this program can't run without it.";

our $state = 'empty';

while(<$fifo_fh>){
  chomp;
  print "exec<$_>\n";
  workConfBindings($_);
}

sub reloadConf {
 open CONFIG, "$fichier_conf" or die "Couldn't read config file $fichier_conf: $!\n";
  while (<CONFIG>) {
      next if (/^#|^\s*$/);  # skip blanks and comments
      chomp;
      my ($variable, $value) = split /=/;
      $config{$variable} = $value;
  }
  close CONFIG;
}


sub workConfBindings {
 $_ = shift;
 
 switch ($_) {
      
      # differents codes
      case '_reloadconf' {
	print "reloadConf \n";
	reloadConf();
	return;
      }
  }
  
 my $action = $config{$_};
 if(!$action){return;}
 print "action< $action >\n";
 system ($action);
}


sub workbindings {
  $_ = shift;
  
  switch ($_) {
      
      # differents codes
      case 'wnd_gauche' { system ('xdotool  key  "ctrl+super+Left"'); }
      case 'wnd_droit' { system ('xdotool  key  "ctrl+super+Right"'); }
      case 'wnd_maximize' { system ('xdotool  key  "ctrl+alt+84"');}
      case 'wnd_minimize' { system ('xdotool  key  "ctrl+alt+90"'); }
      case 'wnd_select_view' { system ('xdotool  key  "shift+super+s"'); }
   
  }
}


